//
//  TableView.swift
//  TomatoClock
//
//  Created by DaleGuo on 2023/11/22.
//

import SwiftUI
import SwiftUICharts

struct TableView: View {
    @EnvironmentObject var vm: HomeViewModel
    
    var body: some View {
        GeometryReader(content: { geo in
            
            NavigationStack {
                BarChartView(data: ChartData(values: formatDataInTable()), title: "Week", form: CGSize(width: 350, height: 200), dropShadow: true, cornerImage: Image(systemName: "chart.bar.xaxis"), animatedToBack: true)
                    .padding()
                
                HStack(spacing: 30) {
                    Button(action: {}, label: {
                        Text("<")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .frame(width: 130, height: 40)
                            .background(Color.accentColor.opacity(0.5))
                            .cornerRadius(10)
                    })
                    Button(action: {}, label: {
                        Text(">")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .frame(width: 130, height: 40)
                            .background(Color.accentColor.opacity(0.5))
                            .cornerRadius(10)
                    })
                }
                
//                HStack {
//                    Text("🗓️")
//                        .bold()
//                    Spacer()
//                    Text("⌛️")
//                    Spacer()
//                    Text("🍅")
//                }
//                .padding(.horizontal)
                
                List {
                    ForEach(vm.weekData) { item in
                        HStack {
                            Text(item.created_time_!.formatted())
                            Spacer()
                            Text("\(item.clock_time_) mins")
                            Spacer()
                            Text("\(item.tomato_num_)")
                        }
                    }
                }
                .listStyle(.plain)
                .navigationTitle("Histoty Data")
            }
            
        })
    }
    
    private func formatDataInTable() -> [(String, Int)] {
        var list: [(String, Int)] = []
        
        for pair in vm.chartData.sorted(by: { $0.key < $1.key
            
        }) {
            list.append((pair.key.formatted(date: .numeric, time: .omitted), pair.value))
        }
        
        return list
    }
}

#Preview {
    TableView()
        .environmentObject(HomeViewModel())
}
